package com.skytech.component.crawler.parse;

import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.scratch.*;
import com.skytech.component.crawler.util.ElementAbstractUrl;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class PartTask {

    @Autowired
    private CrawlerUrlDAO crawlerUrlDAO;
    @Autowired
    private ParseHtmlService parseHtmlService;
    @Autowired
    private PageContentDAO pageContentDAO;
    @Autowired
    private RestTemplateBuilder restTemplateBuilder;
    @Value("${elasticsearch.server}")
    private String server;

    @Value("${chrome.driver.path}")
    public String driverPath;

    private static final Logger log = LoggerFactory.getLogger(ParserLoader.class);

    @Async
    public void partTask(List<CrawlerUrl> crawlerUrls, Map<String, TargetRules> rulesMap, Map<String, List<CustomLocation>> customLocationMap, Integer index, HtmlDownloader htmlDownloader, ISaveData simpl) {

        //log.info("{},task--{}--任务开始",crawlerUrls.get(0).getRuleId(), index);
       // PageContent pageContent = null;
        for (CrawlerUrl url : crawlerUrls) {
            if (!rulesMap.containsKey(url.getRuleId().trim())) {
                continue;
            }
            try {
                Document document = null;
                try {
                    document = htmlDownloader.download(url.getContentUrl());
                } catch (Exception e) {
                    log.error("{},规则 : {}--url : {}--错误原因 : {}",  url.getRuleId(),url.getRuleId(), url.getContentUrl(), e.getMessage());
                    continue;
                }
                Elements allElements = document.getAllElements();
                ElementAbstractUrl.transform(allElements);

                JSONObject jsonObject = new JSONObject();
                TargetRules targetRules = rulesMap.get(url.getRuleId());
                if (!customLocationMap.isEmpty()) {
                    List<CustomLocation> customLocationList = customLocationMap.get(url.getRuleId());
                    if (!customLocationList.isEmpty()) {
                        for (CustomLocation c : customLocationList) {
                            String custom = parseHtmlService.parseTitle(allElements, c.getSelectDescribe());
                            jsonObject.put(c.getCustomSelect(), custom);
                        }
                        jsonObject.put("id",UUID.randomUUID().toString());
                        jsonObject.put("timestamp",LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        jsonObject.put("sourceUrl",url.getContentUrl());
                        jsonObject.put("ruleId",url.getRuleId());
                        jsonObject.put("urlId",url.getId());
                        jsonObject.put("dataType",targetRules.getDataType().trim());
                    }
                }

               // TargetRules targetRules = rulesMap.get(url.getRuleId());
       /*         String title="";
                String content="";
                if(!targetRules.getTitleSelect().isEmpty()){
                    title = parseHtmlService.parseTitle(allElements, targetRules.getTitleSelect());
                }
                if(!targetRules.getContentSelect().isEmpty()){
                    content = parseHtmlService.parseContent(allElements, targetRules.getContentSelect());
                }*/
                 /*if(!StringUtils.isEmpty(content))
                    content = new String(content.getBytes(),"ISO-8859-1");*/
               // PageView pageView = new PageView();
              /*  pageContent = new PageContent();
                pageContent.setId(UUID.randomUUID().toString());
                //pageContent.setContent(content);
                if (jsonObject.isEmpty()) {
                } else {
                    pageContent.setDataItem(jsonObject.toString());
                }
                *//* jsonObject.*//*
                //pageContent.setTitle(title);
                pageContent.setTimestamp(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                pageContent.setSourceUrl(url.getContentUrl());
                pageContent.setRuleId(url.getRuleId());
                pageContent.setUrlId(url.getId());
                pageContent.setDataType(targetRules.getDataType());*/
/*
                pageView.setId(UUID.randomUUID().toString());
                pageView.setContent(content);
                pageView.setTitle(title);
                if (jsonObject.isEmpty()) {
                } else {
                    pageView.setDataItem(jsonObject.toString());
                }
                pageView.setTimestamp(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                pageView.setSourceUrl(url.getContentUrl());
                pageView.setRuleId(url.getRuleId());
                pageView.setUrlId(url.getId());*/

                url.setParse("yes");
                try {
                    simpl.saveData(jsonObject);
                    // customSave(pageContent,url,pageView,index,simpl);
                    crawlerUrlDAO.save(url);
                    log.info("{},task--{}--解析完成--{}", url.getRuleId(), index, url.getContentUrl());
                } catch (Exception e) {
                    log.error("{},task--{}--{}--解析失败--cause{}", url.getRuleId(), index, url.getContentUrl(), e.getMessage());
                }
            } catch (Exception e) {
                log.error("{},task--{}--页面链接错误--{}--info--{}", url.getRuleId(), index, url.getContentUrl(), e.getMessage());
            }
            // todo more field
        }
        //log.info("{},task--{}--任务结束",crawlerUrls.get(0).getRuleId(), index);
        /* driver.close();*/
    }

/*
    @Transactional
    protected void customSave(PageContent pageContent, CrawlerUrl url,PageView pageView,Integer index,ISaveData simpl){


        crawlerUrlDAO.save(url);
        log.info("task--{}--解析完成--{}",index,url.getContentUrl());
    }
*/

    /*public void saveToExcel(){}*/
}
